<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {

	private $model = null;

	public function __construct(){
		parent::__construct();
		$this -> model = M('meeting_room');
	}
	//post提交添加会议室   get 展示后台首页
	public function index(){

		header('Content-type:text/html;charset=utf8;');
		if(IS_POST){
			$name                 = I('post.name');
			$site                     = I('post.site');
			$contain_body    = I('post.contain_body');
			$type                   = I('post.type');
			$use_time0        = I('post.use_time0');
			$use_time1        = I('post.use_time1');
			$use_time2        = I('post.use_time2');
			$intro                   = I('post.intro');
			$use_time = '上午/'.$use_time0.';下午/'.$use_time1.';晚上/'.$use_time2;
			$data = ['name'=>$name,'site'=>$site,'contain_body'=>$contain_body,'type'=>$type,'intro'=>$intro,'use_time'=>$use_time];
			$result = $this -> model -> add($data);
			if(!$result){
				$this -> error('添加数据失败');
			}else{
				$this -> redirect('lists');
			}
		}else{

			$this -> display('form_wizard');
		}
		
	}

	//会议室列表
	public function lists(){
		$data = $this -> model ->select();
		$this -> assign('data',$data);
		$this -> display('editable_table');
	}

	//ajax 删除会议室 

	public function del(){
		$id = I('get.id');
		$result = $this -> model -> delete($id);
		if($result){
			echo 1;
		}else{
			echo 0;
		}
	}

	//编辑会议室状态

	public function edit(){
		header('Content-type:text/html;charset=utf8;');
		if(IS_POST){
			$id                        = I('post.id'); 
			$name                 = I('post.name');
			$site                     = I('post.site');
			$contain_body    = I('post.contain_body');
			$type                   = I('post.type');
			$use_time0        = I('post.use_time0');
			$use_time1        = I('post.use_time1');
			$use_time2        = I('post.use_time2');
			$intro                   = I('post.intro');
			$use_time = '上午/'.$use_time0.';下午/'.$use_time1.';晚上/'.$use_time2;
			$data = ['name'=>$name,'site'=>$site,'contain_body'=>$contain_body,'type'=>$type,'intro'=>$intro,'use_time'=>$use_time];
			$result = $this -> model ->where("id=$id")->save($data);
			if(!$result){
				$this -> error('修改数据失败');
			}else{
				$this -> redirect('lists');
			}
		}else if(IS_GET){
				$id = I('get.id');
				$data = $this -> model -> find($id);
				$use_time = $data['use_time'];
				$time = explode(';', $use_time);
				$this -> assign('time',$time);
				$this -> assign('data',$data);
				$this -> display();  
			}
		}      
		
}



